﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Survey.Entity;
using Survey.DataAccessLayer;

namespace WebUI.Business
{
    /// <summary>
    /// 重构业务逻辑
    /// </summary>
    public class UserBusiness
    {
        #region 用户管理
        private UserAccessor userAccessor = new UserAccessor();
        // Api/LoginUser
        public Object LoginUser(HttpSessionStateBase session,User user)
        {
            if (user.PasswordHash == "" || user.Email == "")
                return new { Result = false ,Msg = "密码不能为空"};
            //验证失败将返回异常
            try
            {
                User tempUser = userAccessor.UserValidation(user.Email, user.PasswordHash);
                session["curentUserId"] = tempUser.Id;
                return new { Result = true };
            }
            catch (System.Exception)
            {
                return new { Result = false ,Msg = "登陆失败"};
            }
        }
        // Api/EditUser
        public Object EditUser(HttpSessionStateBase session,User tempUser)
        {
            bool isOk = false;
            if (string.IsNullOrEmpty(tempUser.PasswordHash) || string.IsNullOrEmpty(tempUser.Email))
            {
                return new { Result = false };
            }


            var curentUserId =session["curentUserId"];
            //没有登陆
            if (curentUserId == null)
            {
                return new { Result = false, Msg = "没有登陆" };
            }
            try
            {

                User user = userAccessor.GetUser((int)curentUserId);
                user.Email = tempUser.Email;
                isOk = userAccessor.ModifyPassword(user, tempUser.PasswordHash);
                return new { Result = isOk, Msg = "编辑成功" };
            }
            catch (Exception e)
            {

                return new { Result = false, Msg = e.Message };

            }

        }
        //Api/regUser
        public Object RegUser(User user)
        {
            if (Units.DataProcess.EmailRegexCheck(user.Email))
            {
                return new { Result = false, Msg = "邮箱不正确" };
            }
            if (Units.DataProcess.PaswordRegexCheck(user.PasswordHash))
            {
                return new { Result = false, Msg = "密码过于简单，必须由单词字符+某些特殊字符组合" };
            }
            
            if (String.IsNullOrEmpty(user.Email) || String.IsNullOrEmpty(user.PasswordHash))
            {
                return new { Result = false, Msg = "密码不能为空" };
            }
            try
            {

                userAccessor.Add(user.Email, user.PasswordHash, new List<Questionnaire>());
                bool isOk = userAccessor.Save() > 0;
                return new { Result = isOk,Msg = "成功" };
            }
            catch (Exception e)
            {
                return new { Result = false, Msg = e.Message };
            }
        }
        //api/LoginOutUser
        public Object LoginOutUser(HttpSessionStateBase session)
        {
            //获取当前用户的在Session里的Id
            var curentUserId = session["curentUserId"];
            if (curentUserId == null)
            {
                return new { Result = false, Msg = "请登陆" };
            }
            session["curentUserId"] = null;
            return new { Result = true };
        }
        // api/DetailedUser
        public Object DetailedUser(HttpSessionStateBase session)
        {
            var curentUserId = session["curentUserId"];

            if (curentUserId == null)
                return new User { Id = -1, Email = "没有这个用户", PasswordHash = "没有这个用户", QuestionnaireCollection = null };
            try
            {
                var user = userAccessor.GetUser((int)curentUserId);
                return Units.DataProcess.Obj2Json(user);
            }
            catch (Exception)
            {
                return new User { Id = -1, Email = "没有这个用户", PasswordHash = "没有这个用户", QuestionnaireCollection = null };
            }
            
        }
        #endregion
    }
}